package com.venn.controller;

import com.venn.entity.NaviPage;
import com.venn.entity.ToDoList;
import com.venn.entity.User;
import com.venn.service.NaviPageService;
import com.venn.util.CommonMethod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Venn on 2017/8/26.
 *
 * 读取一列数据，按照长度拆成6列，除了最后一行，其余行列数相同
 */
@Controller
@RequestMapping("/navipage")
public class NaviPageController {
    private static Logger logger = LogManager.getLogger(UserController.class.getName());
    private CommonMethod commonMethod = new CommonMethod();
    @Autowired
    public NaviPageService naviPageService;
    public String init(HttpSession session){

        return "";
    }

    /**
     * navi page find all navi item
     * @param session
     * @return
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    @ResponseBody
    public List<HashMap<String,NaviPage>> findAll1(HttpSession session){
        // get user id
        String userId = ((User)session.getAttribute("user")).getId();
        // find all navi page
        List<HashMap<String,NaviPage>> naviPages = naviPageService.findAll(userId);
        // return
        return naviPages;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(@RequestParam("title") String title,@RequestParam("url")String url, HttpSession session){
        logger.info("create todolist ");
        String userId = ((User)session.getAttribute("user")).getId();
        NaviPage naviPage = new NaviPage();
        naviPage.setTitle(title);
        naviPage.setUrl(url);
        naviPage.setUserId(userId);
        int id = naviPageService.add(naviPage);

        Map<String, Object> result = new HashMap<>();
        result.put("id",id);

        return commonMethod.returnMsg("0","success");
    }

}
